package model;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import view.Console;

/**
 * An abstract class to read data files in which each line contains the
 * fields separated by a delimiter character, such as tab-delimited or
 * csv files.
 * @author Niel Lebeck
 *
 */
public abstract class DelimitedReader extends DataReader {

    @Override
    public final List<WebsiteVisit> readData(final File dataFile)
        throws WebHistoryDataException {
        List<WebsiteVisit> dataList = new ArrayList<WebsiteVisit>();

        Scanner input = getScanner(dataFile);
        //first line of csv and txt files contain header info that is not needed
        input.nextLine();

        int visitCounter = 0;
        while (input.hasNextLine()) {
            visitCounter++;
            String str = input.nextLine();
            String[] data = str.split(getDelimiter());
            dataList.add(new WebsiteVisit(data));
        }
        Console.appendConsoleText("Success! You loaded " + visitCounter +
                " history events");
        return dataList;
    }

    /**
     * Template method.
     * @return the delimiter character of the DelimitedReader subclass
     */
    public abstract String getDelimiter();
}
